
** South Africa
** Clean Wave 2

*************
*************

	** ROSTER
		use "$dir/rawdata/NIDS South Africa/wave2/HouseholdRoster_W2_Anon_V4.0.0.dta", clear
		
		gen w2_inhh=w2_r_pres==1 if !missing(w2_r_pres)
					
	** Household size
		gen temp=1 if w2_inhh==1
		bys w2_hhid: egen w2_hh_size=total(temp)
		drop temp
		
	** Marital status
		gen w2_married=w2_r_marstt==1 if !missing(w2_r_marstt)

	** Save
		keep w2_hhid pid w2_inhh w2_hh_size w2_married
		tempfile roster
		save `roster', replace
		
*************
*************

	** INDIVIDUAL FILES (DERIVED)
		use "$dir/rawdata/NIDS South Africa/wave2/indderived_W2_Anon_V4.0.0.dta", clear
		
	** Female
		gen w2_female=w2_best_gen==2 if !missing(w2_best_gen) & w2_best_gen>0
		
	** Age
		gen w2_age=w2_best_age_yrs
		
	** Works
		gen w2_work_any_c=w2_empl_stat==3 if !missing(w2_empl_stat)
		
		keep w2_hhid pid w2_age w2_work_any_c w2_female
		tempfile indderiv
		save `indderiv', replace
			 
	** INDIVIDUAL FILES
		use "$dir/rawdata/NIDS South Africa/wave2/Adult_W2_Anon_V4.0.0.dta", clear
		
	** Employment
		gen w2_work_primaryocc=w2_a_em1hrs>0 if !missing(w2_a_em1hrs)
		gen w2_work_secondaryocc=w2_a_em2==1 if !missing(w2_a_em2) & w2_a_em2>0
		gen w2_work_selfemp=w2_a_ems==1 if !missing(w2_a_ems) & w2_a_ems>0
		gen w2_work_casual=w2_a_emc==1 if !missing(w2_a_emc) & w2_a_emc>0
		gen w2_work_agri=w2_a_emp==1 if !missing(w2_a_emp) & w2_a_emp>0
		gen w2_work_bizhelp=w2_a_emh==1 if !missing(w2_a_emh) & w2_a_emh>0
		
		egen w2_work_any=rowmax(w2_work_primaryocc w2_work_secondaryocc w2_work_selfemp w2_work_casual w2_work_agri w2_work_bizhelp)

	** Work hours
		foreach var in w2_a_em1hrs w2_a_em2hrs w2_a_emshrs w2_a_emchrs w2_a_emphrs w2_a_emphrs w2_a_emhhrs {
			replace `var'=. if `var'<0
		}
		egen w2_work_hrs=rowtotal(w2_a_em1hrs w2_a_em2hrs w2_a_emshrs w2_a_emchrs w2_a_emphrs w2_a_emphrs w2_a_emhhrs)
	
	** CESD
		foreach var in w2_a_emobth w2_a_emomnd w2_a_emodep w2_a_emoeff w2_a_emohope w2_a_emoslp w2_a_emohap w2_a_emolone w2_a_emofear w2_a_emogo {
			recode `var' -8=. -3=. -9=.
			recode `var' 1=0 2=1 3=2 4=3
		}
		replace w2_a_emohope=3-w2_a_emohope
		replace w2_a_emohap=3-w2_a_emohap
		egen w2_cesd_total=rowtotal(w2_a_emobth w2_a_emomnd w2_a_emodep w2_a_emoeff w2_a_emohope w2_a_emofear w2_a_emoslp w2_a_emohap w2_a_emolone w2_a_emogo)
		egen w2_cesd_count=rownonmiss(w2_a_emobth w2_a_emomnd w2_a_emodep w2_a_emoeff w2_a_emohope w2_a_emofear w2_a_emoslp w2_a_emohap w2_a_emolone w2_a_emogo)
		replace w2_cesd_total=. if w2_cesd_count<8 // don't score if more than 2 are missing
		gen w2_depressed=w2_cesd_total>=10
		
	** DISABILITIES
		tab1 w2_a_hlvisaid w2_a_hlhraid 
		tab1 w2_a_hlvisaid w2_a_hlhraid, nol

	** Difficulty seeing
		gen w2_diff_seei = w2_a_hlvisaid
		replace w2_diff_seei = 0 if w2_a_hlvisaid==2
		replace w2_diff_seei = . if w2_a_hlvisaid < 0
		tab1 w2_diff_seei

	** Difficulty hearing
		gen w2_diff_hear = w2_a_hlhraid
		replace w2_diff_hear = 0 if w2_a_hlhraid==2
		replace w2_diff_hear = . if w2_a_hlhraid < 0
		tab1 w2_diff_hear

	** Difficulty walking or climbing stairs	
		tab1 w2_a_hldifstrs  w2_a_hldifwlk
		tab1 w2_a_hldifstrs  w2_a_hldifwlk, nol

		gen diff_walk_more_me=1 if inlist(w2_a_hldifwlk, 2, 3, 4)
		replace diff_walk_more_me = 0 if inlist(w2_a_hldifwlk, 1, 5)
		replace diff_walk_more_me = . if w2_a_hldifwlk < 0

		gen diff_walk_stairs=1 if inlist(w2_a_hldifstrs, 2, 3, 4)
		replace diff_walk_stairs = 0 if inlist(w2_a_hldifstrs, 1, 5)
		replace diff_walk_stairs = . if w2_a_hldifstrs < 0

		gen w2_diff_walk_clim=(																	 ///
			diff_walk_stairs==1 | diff_walk_more_me==1                                       	 ///
			)

		replace w2_diff_walk_clim=. if (														 ///
			missing(diff_walk_stairs) &														     ///
			missing(diff_walk_more_me) 													         ///
			)
																								
		tab1 w2_diff_walk_clim

	** Difficulty remembering or concentrating
		*na

	** Difficulty with self-care

		tab1 w2_a_hldifdre w2_a_hldifbath w2_a_hldifeat w2_a_hldiftoil w2_a_hldifcook
		tab1 w2_a_hldifdre w2_a_hldifbath w2_a_hldifeat w2_a_hldiftoil  w2_a_hldifcook, nol

		gen diff_dress=1 if inlist(w2_a_hldifdre, 2, 3, 4)
		replace diff_dress = 0 if inlist(w2_a_hldifdre, 1, 5)
		replace diff_dress = . if w2_a_hldifdre < 0

		gen diff_bath=1 if inlist(w2_a_hldifbath, 2, 3, 4)
		replace diff_bath = 0 if inlist(w2_a_hldifbath, 1, 5)
		replace diff_bath = . if w2_a_hldifbath < 0

		gen diff_eati=1 if inlist(w2_a_hldifeat, 2, 3, 4)
		replace diff_eati = 0 if inlist(w2_a_hldifeat, 1, 5)
		replace diff_eati = . if w2_a_hldifeat < 0

		gen diff_toil=1 if inlist(w2_a_hldiftoil, 2, 3, 4)
		replace diff_toil = 0 if inlist(w2_a_hldiftoil, 1, 5)
		replace diff_toil = . if w2_a_hldiftoil < 0

		gen diff_cook=1 if inlist(w2_a_hldifcook, 2, 3, 4)
		replace diff_cook = 0 if inlist(w2_a_hldifcook, 1, 5)
		replace diff_cook = . if w2_a_hldifcook < 0

		gen w2_diff_self_care=(																	///
			diff_dress==1 | diff_bath==1 | diff_eati==1 | diff_toil==1 | diff_cook==1         	///
			)

		replace w2_diff_self_care=. if (														///
			missing(diff_dress) &														     	///
			missing(diff_bath) &																///
			missing(diff_eati) &																///
			missing(diff_toil) &																///
			missing(diff_cook) 																	///
			)
	** Difficulty communicating
		*na
		
		keep w2_hhid pid w2_work_any w2_work_hrs w2_work_primaryocc w2_work_secondaryocc w2_work_selfemp w2_work_casual w2_work_agri w2_work_bizhelp w2_cesd_count w2_depressed w2_diff_seei w2_diff_hear w2_diff_walk_clim w2_diff_self_care
		tempfile indiv
		save `indiv', replace
		
*************
*************

	** HOUSEHOLD DATA 

		use "$dir/rawdata/NIDS South Africa/wave2/hhderived_W2_Anon_V4.0.0.dta", clear
		
		gen w2_urban=w2_geo2011==2 if w2_geo2011>0 & !missing(w2_geo2011)
		
		gen w2_hh_size_check=w2_hhsizer
		keep w2_hhid w2_hh_size_check w2_urban
		
		tempfile hh
		save `hh', replace
		
*************
*************

	** APPEND
		
		use `hh', clear
		merge 1:m w2_hhid using `roster', assert(matched) nogen
		merge 1:1 w2_hhid pid using `indderiv', assert(master matched) gen(merge_indderiv)
		merge 1:1 w2_hhid pid using `indiv', assert(master matched) gen(merge_indiv)
		
		order w2_hhid pid
		drop merge*
		
		compare w2_work_any_c w2_work_any
		drop w2_work_any_c
		compare w2_hh_size_check w2_hh_size
		drop w2_hh_size_check
		
		save "$dir/data/saf_w2_individual.dta", replace
		
	** Missing variables: 
	** Urban status
	
	** Illness and injury

	** Impact of illness and injury
		